package com.w7vm.menuweb.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.w7vm.menuweb.entity.CardapioBuffetWeb;
import com.w7vm.menuweb.entity.CardapioSelfServiceWeb;
import com.w7vm.menuweb.entity.RestauranteFilialWeb;
import com.w7vm.menuweb.entity.RestauranteSedeWeb;

@Stateless
public class CardapioSelfServiceDAO extends GenericDAO<CardapioSelfServiceWeb> {

	
	@PersistenceContext
	private EntityManager em;
	
	public CardapioSelfServiceDAO() {
		super(CardapioSelfServiceWeb.class);
	}


	public int saveCardapioSelfService(CardapioSelfServiceWeb selfServiceEntity) {
		System.out.println("[IAM-EJB][saveCardapioSelfService] :");
		em.persist(selfServiceEntity);
		em.flush();
		return selfServiceEntity.getIdCardapioSelfServiceWeb();
	}

	public CardapioSelfServiceWeb mergeUpdate(CardapioSelfServiceWeb selfServiceEntity) {
		System.out.println("[IAM-EJB][mergeUpdate] :"+ selfServiceEntity.getIdCardapioSelfServiceWeb());
		return super.update(selfServiceEntity);
	}

	public void deleteCardapioSelfService(int id) {
		System.out.println("[IAM-EJB][deleteCardapioSelfService] :"+ id);
		CardapioSelfServiceWeb selfServiceEntity = new CardapioSelfServiceWeb(); 
		selfServiceEntity = em.find(CardapioSelfServiceWeb.class, id);
		em.remove(selfServiceEntity);
		em.flush();
	}

	public List<CardapioSelfServiceWeb> findCardapioSelfServiceByIDRestauranteSede(int id) {
		System.out.println("[IAM-EJB][findCardapioSelfServiceByIDRestauranteSede] :"+ id);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", id);
		try{
			return super.findByCriterion(CardapioSelfServiceWeb.FIND_SELFSERVICE_BY_IDRESTAURANTESEDE, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}

	public List<CardapioSelfServiceWeb> findCardapioSelfServiceByIDRestauranteFilial(int id) {

		System.out.println("[IAM-EJB][findCardapioSelfServiceByIDRestauranteFilial] :"+ id);
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", id);
		try{
			return super.findByCriterion(CardapioSelfServiceWeb.FIND_SELFSERVICE_BY_IDRESTAURANTEFILIAL, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}
	
	public CardapioSelfServiceWeb findCardapioSelfServiceByID(int entityID) {
		System.out.println("[CARDAPIO-G1][findCardapioSelfServiceByID] :"+ entityID);
		CardapioSelfServiceWeb objSelfServiceEnty = new CardapioSelfServiceWeb();
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idCardapioSelfServiceWeb", entityID);
		try{
			//objSelfServiceEnty = super.find(entityID);
			
			objSelfServiceEnty = super.findOneResult(CardapioSelfServiceWeb.FIND_SELFSERVICE_BY_ID, parameters);
			System.out.println("[CARDAPIO-G1][findCardapioSelfServiceByID  1 ] :"+ objSelfServiceEnty.getComposicaoCardSelfServiceWebs().size());
			
			return objSelfServiceEnty;
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}
	
	public int alterarStatusCardapioSelfService(CardapioSelfServiceWeb selfServiceEntity) {
		System.out.println("[IAM-EJB][alterarStatusCardapioSelfService] :"+ selfServiceEntity.getIdCardapioSelfServiceWeb());

		try{
			Query q1 = em.createQuery("UPDATE CardapioSelfServiceWeb SET diasSemana = ?, dtInicio = ?, dtTermino = ?," +
					" status = ? WHERE idCardapioSelfServiceWeb = ?");
	
			q1.setParameter(1, selfServiceEntity.getDiasSemana());
			q1.setParameter(2, selfServiceEntity.getDtInicio());
			q1.setParameter(3, selfServiceEntity.getDtTermino());
			q1.setParameter(4, selfServiceEntity.getStatus());
			q1.setParameter(5, selfServiceEntity.getIdCardapioSelfServiceWeb());
			
			int updated = q1.executeUpdate();
			return updated;

		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return 0;
		}
	}

	
}
